package com.kzd.btgame.main.home.adapter

import android.graphics.Rect
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ConvertUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kzd.btgame.R
import com.kzd.btgame.entity.GameBean
import com.kzd.btgame.entity.GameGroup
import com.kzd.btgame.entity.GameReserve
import com.kzd.btgame.entity.GameTravel
import com.kzd.btgame.main.game.adapter.Game1Adapter
import com.kzd.btgame.main.game.adapter.GameDetailsImageAdapter
import com.kzd.btgame.main.game.adapter.GameListLabelAdapter
import com.module.lemlin.image.loadImage

class TravelTodayAdapter :
    BaseQuickAdapter<GameGroup, BaseViewHolder>(R.layout.item_travel_today) {
    override fun convert(holder: BaseViewHolder, item: GameGroup) {
        holder.setText(R.id.tvTravelTodayTitle, item.title)
            .getView<RecyclerView>(R.id.rvTravelTodayBody).apply {
                this.layoutManager = LinearLayoutManager(context)
                this.setItemViewCacheSize(item.list.size)
                this.setHasFixedSize(true)
                this.isNestedScrollingEnabled = false
            }
            .adapter = Game1Adapter<GameBean>().apply {
            setNewInstance(item.list)
        }
    }
}

class TravelReserveAdapter :
    BaseQuickAdapter<GameReserve, BaseViewHolder>(R.layout.item_travel_reserve) {
    init {
        addChildClickViewIds(R.id.tvTravelReserveSubmit)
    }

    override fun convert(holder: BaseViewHolder, item: GameReserve) {
        holder.setText(R.id.tvTravelReserveName, item.name)
            .setText(R.id.tvTravelReserveType, "${item.type} · ${item.number}人预约")
            .getView<ImageView>(R.id.ivTravelReserveIcon)
            .loadImage(src = item.icon ?: R.mipmap.ic_80008, R.mipmap.ic_80008, R.mipmap.ic_80008)

        holder.getView<TextView>(R.id.tvTravelReserveSubmit)
            .text = if (item.is_yuyue) "已预约" else "预约"

        holder.getView<RecyclerView>(R.id.rvTravelReserveBody).apply {
            val layoutManager = LinearLayoutManager(context).apply {
                orientation = LinearLayoutManager.HORIZONTAL
            }
            this.layoutManager = layoutManager
        }.adapter = GameListLabelAdapter().apply {
            setNewInstance(item.biaoqian.toMutableList())
        }

        holder.getView<RecyclerView>(R.id.rvTravelReserveImageBody).apply {
            layoutManager = LinearLayoutManager(context).apply {
                orientation = LinearLayoutManager.HORIZONTAL
            }
            if (itemDecorationCount > 0)
                return
            val mDecoration = object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
                ) {
                    val position: Int = parent.getChildAdapterPosition(view)
                    if (position == state.itemCount - 1) {
                        outRect.right = ConvertUtils.dp2px(0f)
                    } else {
                        outRect.right = ConvertUtils.dp2px(7f)
                    }
                }
            }
            addItemDecoration(mDecoration)
        }.adapter = GameDetailsImageAdapter().apply {
            setNewInstance(item.imgs)
        }
    }
}